<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/3/17
 * Time: 19:48
 */

namespace app\api\home;

use app\api\model\DataModel;

class Banner extends Base
{
    protected $url = "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code";

    /**
     * 小程序新闻接口
     * @return string
     */
    public function article()
    {
//        if ($request::param("token") !== Cache::get("token")) {
//            return json_encode(["code" => 404 , "message" => "小东西又想走后门,进不去气不气"]);
//        }

        $data1 = DataModel::table("hisi_article")->select();

        $data2 = DataModel::table("hisi_article")->where("status","10")->select();

        return json_encode(["code" => 1 , "message" => "成功" , "data" => $data1 , "swiper" => $data2]);
    }

    /**
     * 小程序音乐接口
     * @return string
     * @throws \think\Exception
     */
    public function music()
    {
//        if ($request::param("token") !== Cache::get("token")) {
//            return json_encode(["code" => 404 , "message" => "小东西又想走后门,进不去气不气"]);
//        }
        $postId = $_GET["postId"];

        $result = DataModel::table("hisi_article")->where("postId",intval($postId))->find();

        DataModel::table("hisi_article")->where("postId",intval($postId))->setInc("reading");

        $music = DataModel::table("hisi_music")->where("postId",intval($postId))->find();

        $result["music"] = $music;

        return json_encode(["code" => 1 , "message" => "成功" , "data" => $result]);

    }

    /**
     * 收藏次数增减
     * @return string
     * @throws \think\Exception
     */
    public function collection()
    {
//        if ($request::param("token") !== Cache::get("token")) {
//            return json_encode(["code" => 404 , "message" => "小东西又想走后门,进不去气不气"]);
//        }
        $data = $_POST;
        if (isset($data)) {
            if ($data["collection"] == "true") {
                DataModel::table("hisi_article")->where("postId",intval($data["postId"]))->setInc("collection");
            } elseif ($data["collection"] == "false") {
                DataModel::table("hisi_article")->where("postId",intval($data["postId"]))->setDec("collection");
            }
        }
        return json_encode(["code" => 1 , "message" => "操作成功" , "data" => $data]);
    }
}
